/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import exceptions.GenericException;
import java.util.List;
import model.Content;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 *
 * @author tomasz
 */
public class AccountDAO implements Account{
    
    private SessionFactory sessionFactory;
    private HibernateTemplate hibernateTemplate;

    public void setSessionFactory(SessionFactory  sessionFactory) {
            this.sessionFactory = sessionFactory;
            this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    public void addAccount(model.Account account) {
        try{
            
            this.hibernateTemplate.saveOrUpdate(account);
            
        }catch(DataAccessException accessException){
            
           throw new GenericException(accessException.getMessage() );
           
        }
    }

    
    public List allAccount() {
        
        Session session = this.sessionFactory.openSession();
        List<model.Account> ac =  session.createQuery("from Account").list();
//      List <TaskAssigned> ta = this.hibernateTemplate.find("from TaskAssigned");
        return ac;
        
    }
    
    
     public model.Account findAccountId(int id) {
          
//      model.Account account = (model.Account) this.hibernateTemplate.get(model.Account.class, id);

        Session session = this.sessionFactory.openSession();
        model.Account account = (model.Account) session.createQuery("from model.Account WHERE idAccount="+id).uniqueResult();
        
        return account;

    }

    public void addContent(Content con) {
        try{
            
            this.hibernateTemplate.saveOrUpdate(con);
            
        }catch(DataAccessException accessException){
            
           throw new GenericException(accessException.getMessage() );
           
        }
    }

    public void editAccount(model.Account account) {
        try{
            
            this.hibernateTemplate.update(account);
            
        }catch(DataAccessException accessException){
            
           throw new GenericException(accessException.getMessage() );
           
        }
    }

    public List allAccountDashboard() {
        
        Session session = this.sessionFactory.openSession();
        List<model.Account> ac =  session.createQuery("from Account").list();

        return ac;
        
    }
    
}
